<?php
class X2FlowRecordCreate extends X2FlowAction {
	public $title = 'Create Record';
	public $info = '';

	public function paramRules() {
		return array(
			'title' => $this->title,
			'modelClass' => 'modelClass',
			'options' => array(
				array('name'=>'attributes'),
				array('name'=>'modelClass','label'=>Yii::t('studio','Record Type'),'type'=>'dropdown','options'=>X2Model::getModelTypes(true)),
			)
		);
	}

	public function execute(&$params) {
		if(!is_subclass_of($this->config['modelClass'],'X2Model'))	// make sure this is a valid model type
			return array (false, "");
		if(!isset($this->config['attributes']) || empty($this->config['attributes']))
			return array (false, "");

		$model = new $this->config['modelClass'];
		if ($this->setModelAttributes($model,$this->config['attributes'],$params) && $model->save()) {
            return array (
                true,
                Yii::t('studio', 'View created record: ').$model->getLink ());
        } else {
            return array (false, $model->getError ());
        }
	}
}
